package com.youlai.boot.system.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.SysDianciProductionRetrospectMapper;
import com.youlai.boot.system.service.SysDianciProductionRetrospectService;
import com.youlai.boot.system.model.entity.SysDianciProductionRetrospect;
import com.youlai.boot.system.model.form.SysDianciProductionRetrospectForm;
import com.youlai.boot.system.model.query.SysDianciProductionRetrospectQuery;
import com.youlai.boot.system.model.vo.SysDianciProductionRetrospectVO;
import com.youlai.boot.system.converter.SysDianciProductionRetrospectConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 生产追溯服务实现类
 *
 * @author liwenbing
 * @since 2025-04-09 14:04
 */
@Service
@RequiredArgsConstructor
public class SysDianciProductionRetrospectServiceImpl extends ServiceImpl<SysDianciProductionRetrospectMapper, SysDianciProductionRetrospect> implements SysDianciProductionRetrospectService {

    private final SysDianciProductionRetrospectConverter sysDianciProductionRetrospectConverter;

    /**
    * 获取生产追溯分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<SysDianciProductionRetrospectVO>} 生产追溯分页列表
    */
    @Override
    public IPage<SysDianciProductionRetrospectVO> getSysDianciProductionRetrospectPage(SysDianciProductionRetrospectQuery queryParams) {
        Page<SysDianciProductionRetrospectVO> pageVO = this.baseMapper.getSysDianciProductionRetrospectPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取生产追溯表单数据
     *
     * @param id 生产追溯ID
     * @return
     */
    @Override
    public SysDianciProductionRetrospectForm getSysDianciProductionRetrospectFormData(Long id) {
        SysDianciProductionRetrospect entity = this.getById(id);
        return sysDianciProductionRetrospectConverter.toForm(entity);
    }
    
    /**
     * 新增生产追溯
     *
     * @param formData 生产追溯表单对象
     * @return
     */
    @Override
    public boolean saveSysDianciProductionRetrospect(SysDianciProductionRetrospectForm formData) {
        SysDianciProductionRetrospect entity = sysDianciProductionRetrospectConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新生产追溯
     *
     * @param id   生产追溯ID
     * @param formData 生产追溯表单对象
     * @return
     */
    @Override
    public boolean updateSysDianciProductionRetrospect(Long id,SysDianciProductionRetrospectForm formData) {
        SysDianciProductionRetrospect entity = sysDianciProductionRetrospectConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除生产追溯
     *
     * @param ids 生产追溯ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteSysDianciProductionRetrospects(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的生产追溯数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
